import json, subprocess, os, sys, requests
from tqdm import tqdm

os.makedirs("repos", exist_ok=True)

# ---------- 读取待处理的 (repo, commit) ----------
repos = set()
with open("swebench/test.jsonl", encoding="utf-8") as f:
    for line in f:
        ins = json.loads(line)
        repos.add((ins["repo"], ins["base_commit"]))

# ---------- 工具：运行 shell 命令 ----------
def run(cmd, **kw):
    """包装 subprocess.run，失败直接抛异常"""
    return subprocess.run(cmd, shell=True, check=True, **kw)

# ---------- 主循环 ----------
gitee_url_set=set()
for repo, commit in tqdm(repos, desc="checkout"):
    folder = f"repos/{repo.replace('/', '@')}"

    # 1. 如果目录已存在，直接 checkout（可复用）
    if os.path.exists(folder):
        run(f"cd {folder} && git checkout {commit}")
        continue

    # 2. 尝试码云镜像（最快）
    gitee_url = f"https://gitee.com/mirrors/{repo.split('/')[1]}.git"
    # print(gitee_url)
    gitee_url_set.add(gitee_url)
    # try:
    #     # 先检测码云是否存在该仓库
    #     r = requests.head(gitee_url, timeout=5)
    #     if r.status_code == 200:
    #         run(f"git clone --depth=1 {gitee_url} {folder}")
    #         run(f"cd {folder} && git fetch --unshallow")   # 补全历史
    #         run(f"cd {folder} && git checkout {commit}")
    #         continue
    # except Exception as  e:
    #     print(f"失败.....{e}")
    #     pass   # 失败就回退到官方

    # 3. 码云没有，走官方仓库
    # github_url = f"https://github.com/{repo}.git"
    # run(f"git clone --depth=1 {github_url} {folder}")
    # run(f"cd {folder} && git fetch --unshallow")
    # run(f"cd {folder} && git checkout {commit}")

# print(gitee_url_set)
for e in gitee_url_set:
    print(e)